home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / tvtoys04.zip / TVUTILS.PAS < prev    next >
Pascal/Delphi Source File  |  1993-12-18  |  13KB  |  443 lines

  1. (***************************************************************************
  2.   TVUtils unit
  3.   Validators, odd utilities, TV stuff
  4.   PJB December 14, 1993, Internet mail to d91-pbr@nada.kth.se
  5.   Copyright PJB 1993, All Rights Reserved.
  6.   Free source, use at your own risk.
  7.   If modified, please state so if you pass this around.
  8.  
  9. ***************************************************************************)
  10. unit TVUtils;
  11. {$X+}
  12.  
  13. interface
  14.  
  15.   uses
  16.     Dos,
  17.     App, Dialogs, Menus, MsgBox, Objects, Validate, Views,
  18.     toyPrefs, {$I hcFile}
  19.     toyUtils;
  20.  
  21.   const
  22.     kbCtrlA = $1E01;
  23.     kbCtrlB = $3002;
  24.     kbCtrlC = $2E03;
  25.     kbCtrlD = $2004;
  26.     kbCtrlE = $1205;
  27.     kbCtrlF = $2106;
  28.     kbCtrlG = $2207;
  29.     kbCtrlH = $2308;
  30.     kbCtrlI = $1709;
  31.     kbCtrlJ = $240A;
  32.     kbCtrlK = $250B;
  33.     kbCtrlL = $260C;
  34.     kbCtrlM = $320D;
  35.     kbCtrlN = $310E;
  36.     kbCtrlO = $180F;
  37.     kbCtrlP = $1910;
  38.     kbCtrlQ = $1011;
  39.     kbCtrlR = $1312;
  40.     kbCtrlS = $1F13;
  41.     kbCtrlT = $1414;
  42.     kbCtrlU = $1615;
  43.     kbCtrlV = $2F16;
  44.     kbCtrlW = $1117;
  45.     kbCtrlX = $2D18;
  46.     kbCtrlY = $1519;
  47.     kbCtrlZ = $2C1A;
  48.  
  49.   type
  50.     PByte      = ^Byte;
  51.     PWord      = ^Word;
  52.     PByteArray = ^TByteArray;
  53.     PWordArray = ^TWordArray;
  54.  
  55.  
  56.     (* Validate a path *)
  57.     PPathValidator = ^TPathValidator;
  58.     TPathValidator =
  59.       object (TValidator)
  60.         procedure Error; virtual;
  61.         function  IsValid(const S: String): Boolean; virtual;
  62.       end;
  63.  
  64.     (* Validate file name *)
  65.     PFileValidator = ^TFileValidator;
  66.     TFileValidator =
  67.       object (TPathValidator)
  68.         BadName : Boolean;
  69.         procedure Error; virtual;
  70.         function  IsValid(const S: String): Boolean; virtual;
  71.       end;
  72.  
  73.     (* Validate real number *)
  74.     PRealValidator = ^TRealValidator;
  75.     TRealValidator = object(TFilterValidator)
  76.       Min, Max: Real;
  77.       Width, Decimals : Integer;
  78.       constructor Init(AMin, AMax: Real);
  79.       constructor Load(var S: TStream);
  80.       procedure Error; virtual;
  81.       function  IsValid(const S: String): Boolean; virtual;
  82.       procedure Store(var S: TStream);
  83.       function  Transfer(var S:String; Buffer:Pointer; Flag:TVTransfer):Word; virtual;
  84.     end;
  85.  
  86.     (* Validate hex number, four hex digits *)
  87.     PHexValidator = ^THexValidator;
  88.     THexValidator = object(TFilterValidator)
  89.       Min, Max: Word;
  90.       constructor Init(AMin, AMax:Word);
  91.       constructor Load(var S: TStream);
  92.       procedure Error; virtual;
  93.       function  IsValid(const S: String): Boolean; virtual;
  94.       procedure Store(var S: TStream);
  95.       function  Transfer(var S:String; Buffer:Pointer; Flag:TVTransfer):Word; virtual;
  96.     end;
  97.  
  98.     (* A Longint validator that updates a scrollbar *)
  99.     PSliderValidator = ^TSliderValidator;
  100.     TSliderValidator =
  101.       object (TRangeValidator)
  102.         Slider : PScrollbar;
  103.         constructor Init(AMin, AMax:Longint; ASlider:PScrollbar);
  104.         function IsValidInput(var S:String; SuppressFill:Boolean):Boolean; virtual;
  105.       end;
  106.  
  107.  
  108.   const
  109.     (* Help contexts for Borland's ColorSel dialog *)
  110.     ColorSelHelpCtxList : array [1..7] of Word =
  111.       (hcCancel, hcOK, hctoyCSMonoSelector,
  112.        hctoyCSBackground, hctoyCSForeground,
  113.        hctoyCSItem, hctoyCSGroup);
  114.  
  115.   var
  116.     (* True if the Validator is updating the slider, rather than vice versa *)
  117.     IgnoreSliderMessage : Boolean;
  118.  
  119.  
  120.   (* Display a notice in a box *)
  121.   procedure Notice(const Title, Text:String);
  122.   procedure NoNotice;
  123.  
  124.   (* Add help contexts to existing dialogs without builtin contexts *)
  125.   procedure AddHelpCtx(P:PGroup; HelpCtxList:PWord);
  126.  
  127.   procedure DisposeMenuItems(Items:PMenuItem);
  128.   function  StorePointer(var Save; Ref:Pointer):Pointer;
  129.  
  130.  
  131. (***************************************************************************
  132. ***************************************************************************)
  133. implementation
  134.  
  135.   procedure TPathValidator.Error;
  136.   begin
  137.     MessageBox(^M^C'Invalid path', Nil, mfError+mfOkButton);
  138.   end;
  139.  
  140.  
  141.   (*******************************************************************
  142.     Try to validate a path
  143.   *******************************************************************)
  144.   function TPathValidator.IsValid;
  145.     var
  146.       SR : SearchRec;
  147.   begin
  148.     FindFirst(AddBackslash(S)+'*.*', AnyFile, SR);
  149.     IsValid:=DosError<>3;
  150.   end;
  151.  
  152.  
  153.     (*******************************************************************
  154.     *******************************************************************)
  155.  
  156.   procedure TFileValidator.Error;
  157.   begin
  158.     if BadName then
  159.       MessageBox(^M^C'Invalid file name', Nil, mfError+mfOkButton)
  160.     else
  161.       inherited Error;
  162.   end;
  163.  
  164.  
  165.   (*******************************************************************
  166.     Try to see if it is a valid file name, difficult and not
  167.     quite reliable
  168.   *******************************************************************)
  169.   function TFileValidator.IsValid;
  170.     var
  171.       SR : SearchRec;
  172.   begin
  173.     BadName:=False;
  174.     FindFirst(S, AnyFile-Directory, SR);
  175.     if (DosError=18) or (S[Length(S)]='\') then
  176.     begin
  177.       BadName:=True;
  178.       FindFirst(S+'\*.*', AnyFile, SR);
  179.       IsValid:=DosError=3;
  180.     end
  181.     else
  182.       IsValid:=DosError<>3;
  183.   end;
  184.  
  185.  
  186.     (*******************************************************************
  187.     *******************************************************************)
  188.  
  189.   (*******************************************************************
  190.     Simple real validator
  191.   *******************************************************************)
  192.   constructor TRealValidator.Init;
  193.   begin
  194.     inherited Init(['0'..'9','+','-','.']);
  195.     if AMin >= 0 then ValidChars:=ValidChars - ['-'];
  196.     Min:=AMin;
  197.     Max:=AMax;
  198.   end;
  199.  
  200.   constructor TRealValidator.Load(var S: TStream);
  201.   begin
  202.     inherited Load(S);
  203.     S.Read(Min, SizeOf(Max) + SizeOf(Min));
  204.   end;
  205.  
  206.   procedure TRealValidator.Error;
  207.   var
  208.     Params: array [0..1] of Longint;
  209.   begin
  210.     Params[0]:=Round(Min);
  211.     Params[1]:=Round(Max);
  212.     MessageBox('Value not in the range %d to %d', @Params,
  213.       mfError + mfOKButton);
  214.   end;
  215.  
  216.   function TRealValidator.IsValid(const S: String): Boolean;
  217.   var
  218.     Value: Real;
  219.     Code: Integer;
  220.   begin
  221.     IsValid:=False;
  222.     if inherited IsValid(S) then
  223.     begin
  224.       Val(S, Value, Code);
  225.       if (Code = 0) and (Value >= Min) and (Value <= Max) then
  226.         IsValid:=True;
  227.     end;
  228.   end;
  229.  
  230.   procedure TRealValidator.Store(var S: TStream);
  231.   begin
  232.     inherited Store(S);
  233.     S.Write(Min, SizeOf(Max) + SizeOf(Min));
  234.   end;
  235.  
  236.  
  237.   (*******************************************************************
  238.     Transfer a real
  239.   *******************************************************************)
  240.   function TRealValidator.Transfer(var S: String; Buffer: Pointer; Flag: TVTransfer): Word;
  241.   var
  242.     Value: Real;
  243.     Code: Integer;
  244.   begin
  245.     if Options and voTransfer <> 0 then
  246.     begin
  247.       Transfer:=SizeOf(Value);
  248.       case Flag of
  249.        vtGetData:
  250.          begin
  251.            Val(S, Value, Code);
  252.            Real(Buffer^):=Value;
  253.          end;
  254.        vtSetData:
  255.          Str(Real(Buffer^):Width:Decimals, S);
  256.       end;
  257.     end
  258.     else
  259.       Transfer:=0;
  260.   end;
  261.  
  262.  
  263.     (*******************************************************************
  264.     *******************************************************************)
  265.  
  266.   (*******************************************************************
  267.     Simple hex validator, four hex digits
  268.   *******************************************************************)
  269.   constructor THexValidator.Init;
  270.   begin
  271.     inherited Init(['0'..'9','A'..'F','a'..'f']);
  272.     Options:=Options or voTransfer;
  273.     Min:=AMin;
  274.     Max:=AMax;
  275.   end;
  276.  
  277.   constructor THexValidator.Load(var S: TStream);
  278.   begin
  279.     inherited Load(S);
  280.     S.Read(Min, SizeOf(Max) + SizeOf(Min));
  281.   end;
  282.  
  283.   procedure THexValidator.Error;
  284.   var
  285.     Params: array [0..1] of Longint;
  286.   begin
  287.     Params[0]:=Min;
  288.     Params[1]:=Max;
  289.     MessageBox('Value not in the range %d to %d', @Para